<template>
  <view class="address" @click="addressFn">
    <uv-list v-for="(item, index) in addr" :key="index">
      <uv-list-item
          :title="`${item.consignee}，${item.phone}`"
          :note="item.address"
          :show-arrow="true"
          :thumb="address"
          thumb-size="lg"
      ></uv-list-item>
    </uv-list>
  </view>
</template>
<script setup>
import { ref, onMounted, defineEmits } from "vue";
import address from "@/static/img/addr.png";
import { getAddress } from "@/request/api";

const emit = defineEmits(); // Define the emit function
const addr = ref([]);
const id = ref(0);

onMounted(() => {
  getAddress().then(res => {
    if (res.code === 0) {
      addr.value = res.result.filter(item => item.is_default === true);
      if (addr.value.length > 0) {
        id.value = addr.value[0].id;
        emit('update:id', id.value); // Emit the id value
        // console.log(id.value, '---');
      }
    }
  });
});

const addressFn = () => {
  uni.showLoading({
    title: '加载中...'
  });
  setTimeout(() => {
    uni.hideLoading();
    uni.navigateTo({
      url: '/pages/setting/address/address'
    });
  }, 100);
}
</script>
<style scoped lang="scss">
.address{
  padding: 0 15px;
  box-sizing: border-box;
  background-color: #FFFFFF;
}
</style>